﻿using System;
using VivendoByteToolkit.Enums;
using VivendoByteToolkit.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace VivendoByteToolkit.Controls
{
    public partial class CurrencyPicker : UserControl
    {
        static CurrencyPickerViewModel vm;

        #region Dependency properties

        #region Amount dependency property
        public static decimal GetAmount(DependencyObject obj)
        {
            return (decimal)vm.CurrencyPicker.GetValue(AmountProperty);
        }

        public static void SetAmount(DependencyObject obj, decimal value)
        {
            vm.CurrencyPicker.SetValue(AmountProperty, value);
        }

        public static readonly DependencyProperty AmountProperty =
            DependencyProperty.RegisterAttached("Amount",
            typeof(decimal),
            typeof(CurrencyPicker),
            new PropertyMetadata(0, new PropertyChangedCallback(Update)));
        #endregion

        #region Minimum dependency property
        public static decimal GetMinimum(DependencyObject obj)
        {
            return (decimal)vm.CurrencyPicker.GetValue(MinimumProperty);
        }

        public static void SetMinimum(DependencyObject obj, decimal value)
        {
            vm.CurrencyPicker.SetValue(MinimumProperty, value);
        }

        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.RegisterAttached("Minimum",
            typeof(decimal),
            typeof(CurrencyPicker),
            new PropertyMetadata(0.0M));
        #endregion

        #region Maximum dependency property
        public static decimal GetMaximum(DependencyObject obj)
        {
            return (decimal)vm.CurrencyPicker.GetValue(MaximumProperty);
        }

        public static void SetMaximum(DependencyObject obj, decimal value)
        {
            vm.CurrencyPicker.SetValue(MaximumProperty, value);
        }

        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.RegisterAttached("Maximum",
            typeof(decimal),
            typeof(CurrencyPicker),
            new PropertyMetadata(decimal.MaxValue));
        #endregion

        #region Symbol dependency property
        public static string GetSymbol(DependencyObject obj)
        {
            return vm.CurrencyPicker.GetValue(SymbolProperty).ToString();
        }

        public static void SetSymbol(DependencyObject obj, string value)
        {
            vm.CurrencyPicker.SetValue(SymbolProperty, value);
        }

        public static readonly DependencyProperty SymbolProperty =
            DependencyProperty.RegisterAttached("Symbol",
            typeof(string),
            typeof(CurrencyPicker),
            new PropertyMetadata("€", new PropertyChangedCallback(UpdateSymbol)));
        #endregion 

        #endregion

        #region Public properties
        public decimal Amount
        {
            get
            {
                return CurrencyPicker.GetAmount(this);
            }
            set
            {
                CurrencyPicker.SetAmount(this, value);
            }
        }

        public decimal Minimum
        {
            get
            {
                return CurrencyPicker.GetMinimum(this);
            }
            set
            {
                CurrencyPicker.SetMinimum(this, value);
            }
        }

        public decimal Maximum
        {
            get
            {
                return CurrencyPicker.GetMaximum(this);
            }
            set
            {
                CurrencyPicker.SetMaximum(this, value);
            }
        }

        public string Symbol
        {
            get
            {
                return CurrencyPicker.GetSymbol(this);
            }
            set
            {
                CurrencyPicker.SetSymbol(this, value);
            }
        } 
        #endregion

        public CurrencyPicker()
        {
            this.InitializeComponent();
            vm = this.Resources["viewmodel"] as CurrencyPickerViewModel;
            vm.CurrencyPicker = this;
        }

        private static void Update(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                vm.Amount = (decimal)e.NewValue;
            }
        }

        private static void UpdateSymbol(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                vm.Symbol = e.NewValue.ToString();
            }
        }
    }
}